package ch.zbw.rre;

public class VerketteteListe implements Liste {
	
	private Node head;

	public VerketteteListe() {
		head = new Node();
	}

	@Override
	public boolean insertFirst(Element x) {
		Node neu;
		neu = new Node();
		neu.setItem(x);
		neu.setNext(head);
		head = neu;
		Element temp;
		temp = head.getItem();
		if (temp.getId() == x.getId()){
			return true;
		}
		return false;
	}

	@Override
	public boolean insertLast(Element x) {
		//Falls keine Element vorhanten ist, kann direket 
		//Die Methode INSERTFIRST aufgerufen werden.
		if (empty() == true){
			insertFirst(x);
			return true;
		}
		
		
		if (empty() == false){
			Node temp;
			Node temp2;
			temp = head;
			temp2 = null;
			while(temp != null){
				if ((temp.getNext() == null) && (temp.getItem() == null)){
					Node neu;
					neu = new Node();
					neu.setItem(x);
					neu.setNext(temp2.getNext());
					temp2.setNext(neu);
					return true;
				}
				temp2 =temp;
				temp = temp.getNext();
			}
		}
		return false;
	}

	@Override
	public boolean empty() {
		if(head.getItem() == null && head.getNext() == null){
			return true;
		}
		return false;
	}

	@Override
	public Element getFirst() {
		if (empty() != true){
			return head.getItem();
		}
		return null;
	}

	@Override
	public Element getLast() {
		if (empty() != true){
			Node temp;
			Node temp2;
			temp = head;
			temp2 = null;
			while(temp != null){
				if ((temp.getNext() == null) && (temp.getItem() == null)){
					return temp2.getItem();
				}
				temp2 = temp;
				temp = temp.getNext();
			}
		}

		return null;
	}

	@Override
	public Element search(int id) {
		if(empty() != true){
			Node temp;
			temp = head;
			while (temp.getNext() != null){
				Element tempEl;
				tempEl = temp.getItem();
				if(tempEl.getId() == id){
					return tempEl;
				}
				temp = temp.getNext();
			}
		}
		return null;
	}

	@Override
	public boolean delete(int id) {
		if (countElement() == 0){
			return false;
		}
		
		if (countElement() == 1){
			Element tempElement;
			Node temp1;
			temp1 = head;
			tempElement = head.getItem();
			if (tempElement.getId() == id){
				head = new Node();
				return true;
			}
			
			
		}
		
		if (countElement() >1){
			Node tempVorher;
			Node tempNachher;
			tempVorher = head;
			tempNachher = head;
			while (tempNachher != null){
				Element temp;
				temp = tempNachher.getItem();
				if (temp.getId() == id){
					tempVorher.setNext(tempNachher.getNext());
					return true;
				}
				tempVorher = tempNachher;
				tempNachher = tempNachher.getNext();
				}
		}
		
		return false;
	}

	@Override
	public void show() {
		Node temp;
		temp = head;
		while (temp == null){
			temp.getItem().show();
			temp = temp.getNext();
		}
		
		
	}
	
	public int countElement() {
		int count = -1;
		Node temp;
		temp = head;
		while (temp != null){
			count ++;
			temp = temp.getNext();
		}
		return count;
	}
}
